package com.cctc.rds.scheduler.server.config.helper;

import org.yaml.snakeyaml.Yaml;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class YamlReader<T> {
    private String classpath;
    private Class<T> tClass;

    public YamlReader(String classpath) {
        this.classpath = classpath;
        Type superclass = this.getClass().getGenericSuperclass();
        if (superclass instanceof Class) {
            throw new UnsupportedOperationException("YamlReader 类型异常！！！");
        } else {
            tClass = (Class<T>) ((ParameterizedType) superclass).getActualTypeArguments()[0];
        }
    }

    public T loadConfig() {
        Yaml yaml = new Yaml();
        try (InputStream is = this.getClass().getResourceAsStream(classpath)) {
            return yaml.loadAs(is, tClass);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
